package com.chixing.day16_io.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectAccount {
    public static void main(String[] args) {
        //获得所有的方法 Method
        Class<Account> clazz = Account.class;
        Method[] methods = clazz.getDeclaredMethods();
        for (Method method:methods){
        String name = method.getName();
        Class<?> returnType = method.getReturnType();
        Class<?>[] paramTypes = method.getParameterTypes();
        int modifiers = method.getModifiers();
        Class<?>[] exceptionTypes = method.getExceptionTypes();


        System.out.print("方法名称: " + name);
        System.out.print(", 访问修饰符: " + modifiers);
        System.out.print(", 返回值类型: " + returnType);
        for (Class<?> paramType: paramTypes){
            System.out.print(", 参数列表类型: " + paramType);
        }
        for (Class<?> exceptionType : exceptionTypes){
            System.out.print(",异常类型: " + exceptionType);
        }
            System.out.println("  ");

        }
        //方法调用account.play(int,float) 形参
        Method method = null;
        try {
            method = clazz.getMethod("withdraw",int.class,float.class);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

        Account account = null;
        try {
            account = clazz.newInstance(); //依赖于无参构造函数
            System.out.println("实例化account对象");
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        //arguments
        Object result = null;
        try {
           result = method.invoke(account,10,9.9f);
            System.out.println("返回值:" + result);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }


        //获得所有的字段 Field
        Class<Account> clazz1 = Account.class;
        Field[] fields = clazz1.getDeclaredFields();
        for (Field field : fields){
          String fieldName = field.getName();

            System.out.println("字段名" + fieldName);
        }



    }
}
